<?php
class model_newagk_archivesmark extends gdl_components_basemodel
{

    public function primarykey() {
        return 'id';
    }

    public function tableName() {
        return 'agk_archives_mark';
    } 
	//单用户归档
	public function archives($pid){

		$examspapers = new model_newagk_examspapers();
		$basic = new model_newagk_basic();
		$paper = new model_newagk_paper();
		$platform = new model_newagk_platform();
		$basicuser = new model_newagk_basicuser();
		$usercompany = new model_newagk_usercompany();
		$qbank = new model_newagk_qbank();
		$users = new model_newagk_user();
		$paperDetailed  = $examspapers->getOnes($pid);
		
		
		if($paperDetailed==false || empty($paperDetailed)) return false;
		
		
	//	$this->delete(['identity_id'=>$paperDetailed['identity_id'],'basicId'=>$paperDetailed['basicid']]);
		$basicDetailed  = $basic->getInfoArgOne(['id'=>$paperDetailed['basicid']],'typesId,pid,basic,starttime,endtime,questions,ques_name,eventid,eventName');
		$paperDet  = $paper->getInfo($basicDetailed['pid']);
		$basicuserDetailed  = $basicuser->getInfoArgOne(['basic_id'=>$paperDetailed['basicid'],'identity_id'=>$paperDetailed['identity_id']],'username,usertruename,company,company_id,idcard');
		$usercompanys = $usercompany->getInfoArgOne(['id'=>$paperDetailed['identity_id']],'gdlUserId,platformId,userId,companyName,companyId');
		$userDel = $users->selectOne(['userId'=>$usercompanys['userId']],'birthday,sex,age,idcard,realName,userName');	
		if(!$userDel){
			return true;
		}

		$platformName = $platform->getInfoById($usercompanys['platformId'],'reals');
		$qbankName = $qbank->getListByArg(['id'=>$paperDet['dbs']],'name');
		$data['username'] 		= $userDel['userName']; //用户名,
		$data['usertruename'] 	= $userDel['realName']; //真实名称,
		$data['identity_id'] 	= $paperDetailed['identity_id']; //身份id,
		$data['gdluserId'] 		= $usercompanys['gdlUserId']; //干电力网主网用户 ID,
		$data['idcard']			= $userDel['idcard']; //身份证号,
		$data['platformId'] 		= $usercompanys['platformId']; //平台id,
		$data['platformName'] 	= $platformName['reals']; //平台名,
		$data['companyName'] 	= $usercompanys['companyName']; //公司名称,
		$data['companyId'] 		= $usercompanys['companyId']; //公司id,
		$data['basicId'] 		= $paperDetailed['basicid']; //考场id,
		$data['paperId'] 		= $basicDetailed['pid']; //真实试卷ID,
		$data['duration'] 		= $paperDet['duration']; //考试时长,
		$data['basicName'] 		= $basicDetailed['basic']; //考场名,
		$data['paperName'] 		= $paperDetailed['paperName']; //试卷名字,
		$data['bankId'] 		= $paperDet['dbs']; //题库ID,
		$data['bankName'] 		= $qbankName[0]['name']; //题库名,
		$data['score'] 			= $paperDetailed['modified']; //得分,
		$data['startTime'] 		= $basicDetailed['starttime']; //正式考试开始考试时间,
		$data['endTime'] 		= $basicDetailed['endtime']; //正式考试结束考试时间,
		$data['passscore'] 		= $paperDetailed['passscore']; //通过分数,
		$data['paperScore']		= $paperDet['totalScore']; //试卷总分,
		$data['time'] 			= $paperDetailed['time']; //总用时,
		$data['pass'] 			= $paperDetailed['modified']>=$paperDetailed['passscore'] ? 1 : 0; //是否合格 0未合格 1合格,
		$data['eventid'] 		= $paperDetailed['eventid']?$paperDetailed['eventid']:0; //工程id,
		$data['event_name'] 	= $paperDetailed['eventName']?$paperDetailed['eventName']:''; //当时所属工程,
		$data['questions']		= $paperDetailed['question']; //试卷所有试题,
		$data['questionsNum'] 	= $paperDet['totalNum']; //试题总数,
		$data['answer'] 		= $paperDetailed['answer']; //用户答案,
		$data['usual'] 			= $paperDetailed['type']; //考场状态 1正常 2考试异常 3身份异常,
		$data['remark'] 		= $paperDetailed['remark']; //考试说明,
		$data['terminal']		= $paperDetailed['terminal']; //考试终端 1手机app 2平板app 3pc端,
		$data['pass_questions'] = $paperDetailed['pass_questions']; //答对题数,
		$data['nopass_questions'] = $paperDetailed['nopass_questions']; //答错题数,
		$data['createTime'] = date('Y-m-d H:i:s'); //结束时间,
		$data['birthday'] = $userDel['birthday']; //'生日',
		$data['ages'] = $userDel['age']; //'当时备案时候的年龄',
		$data['sex'] = $userDel['sex']; //'0:男 1 女',
		$this->insert($data);
		return $this;
	//////////////////////
     
    } 
	
	
	//单用户归档【缺考的】
	public function missArchives($id){

		$examspapers = new model_newagk_examspapers();
		$basic = new model_newagk_basic();
		$paper = new model_newagk_paper();
		$platform = new model_newagk_platform();
		$basicuser = new model_newagk_basicuser();
		$usercompany = new model_newagk_usercompany();
		$qbank = new model_newagk_qbank();
		$users = new model_newagk_user();
		$basicuserDetailed  = $basicuser->getInfoArgOne(['id'=>$id],	'username,usertruename,company,company_id,idcard,basic_id,identity_id');
		
		
		if(empty($basicuserDetailed)) return false;
		
		
	//	$this->delete(['identity_id'=>$paperDetailed['identity_id'],'basicId'=>$paperDetailed['basicid']]);
		
	
		$basicDetailed  = $basic->getInfoArgOne(['id'=>$basicuserDetailed['basic_id']],'typesId,pid,basic,starttime,endtime,questions,ques_name,eventid,eventName');
		$paperDet  = $paper->getInfo($basicDetailed['pid']);
	
		$usercompanys = $usercompany->getInfoArgOne(['id'=>$basicuserDetailed['identity_id']],'gdlUserId,platformId,userId,companyName,companyId');
		$userDel = $users->selectOne(['userId'=>$usercompanys['userId']],'birthday,sex,age,userName,realName,idcard');
			if(!$userDel){
				return true;
			}
		if($paperDet['paperType']==2){
				$msg = unserialize($paperDet['datas']);

		} else {
				$drawquestion = new service_drawquestion();
				$msg = $drawquestion->randomDrawQues($paperDet['dbs'],$paperDet['settings'],1);
		}
		$datak=[];
		if(!empty($msg)){
			foreach($msg as $k=>$v){
						foreach($v as $kg=>$vg){
							//对答案进行处理

							$datak[$k][] = [
											'id'=>$vg['id'],
											'typeId'=>$vg['typeId'],
											'question'=>$vg['question'],
											'select'=>$vg['select'],
											'q_select_arr'=>$vg['q_select_arr'],
											'questionImg'=>$vg['questionImg'],
											'qbankId'=>$vg['qbankId'],
											'finalAnswer'=>'',
											'answer'=>$vg['answer'],
											'sectionId'=>$vg['sectionId'],
											'describe'=>$vg['describe'],
											];
						}
			}
		}
		
		$platformName = $platform->getInfoById($usercompanys['platformId'],'reals');
		$qbankName = $qbank->getListByArg(['id'=>$paperDet['dbs']],'name');
		$data['username'] 		= $userDel['userName']; //用户名,
		$data['usertruename'] 	= $userDel['realName']; //真实名称,
		$data['identity_id'] 	= $basicuserDetailed['identity_id']; //身份id,
		$data['gdluserId'] 		= $usercompanys['gdlUserId']; //干电力网主网用户 ID,
		$data['idcard']			= $userDel['idcard']; //身份证号,
		$data['platformId'] 		= $usercompanys['platformId']; //平台id,
		$data['platformName'] 	= $platformName['reals']; //平台名,
		$data['companyName'] 	= $usercompanys['companyName']; //公司名称,
		$data['companyId'] 		= $usercompanys['companyId']; //公司id,
		$data['basicId'] 		= $basicuserDetailed['basic_id']; //考场id,
		$data['paperId'] 		= $basicDetailed['pid']; //真实试卷ID,
		$data['duration'] 		= $paperDet['duration']; //考试时长,
		$data['basicName'] 		= $basicDetailed['basic']; //考场名,paperDetailed
		$data['paperName'] 		= $paperDet['name']; //试卷名字,
		$data['bankId'] 		= $paperDet['dbs']; //题库ID,
		$data['bankName'] 		= $qbankName[0]['name']; //题库名,
		$data['score'] 			= 0; //得分,
		$data['startTime'] 		= $basicDetailed['starttime']; //正式考试开始考试时间,
		$data['endTime'] 		= $basicDetailed['endtime']; //正式考试结束考试时间,
		$data['passscore'] 		= $paperDet['passScore']; //通过分数,
		$data['paperScore']		= $paperDet['totalScore']; //试卷总分,
		$data['time'] 			= $paperDet['duration']; //总用时,
		$data['pass'] 			=  0; //是否合格 0未合格 1合格,
		$data['eventid'] 		= $basicDetailed['eventid']?$basicDetailed['eventid']:0; //工程id,
		$data['event_name'] 	= $basicDetailed['eventName']?$basicDetailed['eventName']:''; //当时所属工程,
		$data['questions']		= serialize($datak); //试卷所有试题,
		$data['questionsNum'] 	= $paperDet['totalNum']; //试题总数,
		$data['answer'] 		= 'a:0:{}'; //用户答案,
		$data['usual'] 			= 4; //考场状态 1正常 2考试异常 3身份异常 4缺考,
		$data['remark'] 		= '缺考'; //考试说明,
		$data['terminal']		= 1; //考试终端 1手机app 2平板app 3pc端,
		$data['pass_questions'] = 0; //答对题数,
		$data['nopass_questions'] = 0; //答错题数,
		$data['createTime'] = date('Y-m-d H:i:s'); //结束时间,
		$data['birthday'] = $userDel['birthday']; //'生日',
		$data['ages'] = $userDel['age']; //'当时备案时候的年龄',
		$data['sex'] = $userDel['sex']; //'0:男 1 女',
		$this->insert($data);
		return true;
	//////////////////////
     
    }
}
